home *** CD-ROM | disk | FTP | other *** search
/ Delphi Informant Complete 1995 - 2000 / Delphi Informant Complete 1995 to 2000.iso / Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar / 1998 / Dec / di9812me / PluginSample / 3 / shell3 / main.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-05-26  |  3.3 KB  |  136 lines

  1. unit main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Menus, StdCtrls, Buttons;
  8.  
  9. const
  10.     cPLUGIN_MASK    = '*.plg';
  11.  
  12. type
  13.   TfrmMain = class(TForm)
  14.     mnuMain: TMainMenu;
  15.     File1: TMenuItem;
  16.     Exit1: TMenuItem;
  17.     GroupBox1: TGroupBox;
  18.     memPlugins: TMemo;
  19.     BitBtn1: TBitBtn;
  20.     Plugin1: TMenuItem;
  21.     procedure Exit1Click(Sender: TObject);
  22.     procedure FormCreate(Sender: TObject);
  23.   private
  24.     { Private declarations }
  25.     procedure LoadPlugins;
  26.     procedure LoadPlugin(sr: TSearchRec);
  27.     procedure MinMaxInfo(var msg: TMessage); message WM_GETMINMAXINFO;
  28.   public
  29.     { Public declarations }
  30.   end;
  31.  
  32. var
  33.   frmMain: TfrmMain;
  34.   lstMinMax: TList;
  35.  
  36. implementation
  37.  
  38. uses Common;
  39.  
  40. {$R *.DFM}
  41.  
  42. procedure TfrmMain.Exit1Click(Sender: TObject);
  43. begin
  44.     frmMain.Close;
  45. end;
  46.  
  47. {Iterate the application directory looking for plugin files}
  48. procedure TfrmMain.LoadPlugins;
  49. var sr: TSearchRec;
  50.     path: string;
  51.     Found: integer;
  52. begin
  53.     path := ExtractFilePath(Application.Exename);
  54.     try
  55.         Found := FindFirst(path+cPLUGIN_MASK, 0, sr);
  56.         while Found = 0 do
  57.         begin
  58.             LoadPlugin(sr);
  59.             Found := FindNext(sr);
  60.         end;
  61.     finally
  62.         FindClose(sr);
  63.     end;
  64. end;
  65.  
  66. {Load the specified plugin DLL}
  67. procedure TfrmMain.LoadPlugin(sr: TSearchRec);
  68. var Description: String;
  69.     LibHandle: integer;
  70.     DescribeProc: TPluginDescribe;
  71.     InitProc: TPluginInit;
  72.     InitEvents: TInitPluginEvents;
  73. begin
  74.     LibHandle := LoadLibrary(Pchar(sr.Name));
  75.     if LibHandle <> 0 then
  76.     begin
  77.         // Find DescribePlugin
  78.         DescribeProc := GetProcAddress(LibHandle, cPLUGIN_DESCRIBE);
  79.         if assigned(DescribeProc) then
  80.         begin
  81.             // Call DescribePlugin
  82.             DescribeProc(Description);
  83.             memPlugins.Lines.Add(Description);
  84.             // Find InitPlugin
  85.             InitProc := GetProcAddress(LibHandle, cPLUGIN_INIT);
  86.             if assigned(InitProc) then
  87.             begin
  88.                 //Call InitPlugin
  89.                 InitProc(mnuMain);
  90.             end;
  91.             // Find InitPluginEvents for the 3rd plugin
  92.             InitEvents := GetProcAddress(LibHandle, cPLUGIN_INITEVENTS);
  93.             if assigned(InitEvents) then
  94.             begin
  95.                 //Call InitPlugin
  96.                 InitEvents(lstMinMax);
  97.             end;
  98.         end
  99.         else
  100.         begin
  101.             Messagedlg('File "'+sr.Name+'" is not a valid plugin.',
  102.                         mtInformation, [mbOK], 0);
  103.         end;
  104.     end
  105.     else
  106.     begin
  107.         Messagedlg('An error occurred loading the plugin "'+sr.Name+'".',
  108.                     mtInformation, [mbOK], 0);
  109.     end;
  110. end;
  111.  
  112. procedure TfrmMain.FormCreate(Sender: TObject);
  113. begin
  114.     LoadPlugins;
  115. end;
  116.  
  117. {Trap for WM_GETMINMAXINFO. Calls plugin routine on every message}
  118. procedure TfrmMain.MinMaxInfo(var msg: TMessage);
  119. var m: PMinMaxInfo; // Defined in Windows.pas
  120.     i: integer;
  121. begin
  122.     m := pointer(msg.Lparam);
  123.     for i := 0 to lstMinMax.count -1 do
  124.     begin
  125.         TResizeProc(lstMinMax[i])(m.ptMinTrackSize.x, m.ptMinTrackSize.y);
  126.     end;
  127. end;
  128.  
  129. initialization
  130.     lstMinMax := TList.Create;
  131.  
  132. finalization
  133.     lstMinMax.Free;
  134.  
  135. end.
  136.